home *** CD-ROM | disk | FTP | other *** search
- //---------------------------------------------------
- // EnumerateEnumeration.cs ⌐ 2001 by Charles Petzold
- //---------------------------------------------------
- using System;
- using System.Drawing;
- using System.Reflection; // Para la clase Assembly
- using System.Text; // Para la clase StringBuilder
- using System.Windows.Forms;
-
- class EnumerateEnumeration: Form
- {
- Button button;
- TextBox tbLibrary, tbNamespace, tbEnumeration, tbOutput;
- CheckBox cbHex;
-
- public static void Main()
- {
- Application.Run(new EnumerateEnumeration());
- }
- public EnumerateEnumeration()
- {
- Text = "Enumerar Enumeraci≤n";
- ClientSize = new Size(242, 164);
-
- Label label = new Label();
- label.Parent = this;
- label.Text = "Biblioteca:";
- label.Location = new Point(8, 8);
- label.Size = new Size(56, 8);
-
- tbLibrary = new TextBox();
- tbLibrary.Parent = this;
- tbLibrary.Text = "system.windows.forms";
- tbLibrary.Location = new Point(64, 8);
- tbLibrary.Size = new Size(120, 12);
- tbLibrary.Anchor |= AnchorStyles.Right;
-
- ToolTip tooltip = new ToolTip();
- tooltip.SetToolTip(tbLibrary,
- "Introduzca el nombre de una biblioteca\n" +
- "de enlace dinßmico .NET, como 'mscorlib',\n" +
- "'system.windows.forms', o\n" +
- "'system.drawing'.");
-
- label = new Label();
- label.Parent = this;
- label.Text = "Espacio de nombres:";
- label.Location = new Point(8, 24);
- label.Size = new Size(56, 8);
-
- tbNamespace = new TextBox();
- tbNamespace.Parent = this;
- tbNamespace.Text = "System.Windows.Forms";
- tbNamespace.Location = new Point(64, 24);
- tbNamespace.Size = new Size(120, 12);
- tbNamespace.Anchor |= AnchorStyles.Right;
-
- tooltip.SetToolTip(tbNamespace,
- "Introduzca el nombre de un espacio\n" +
- "de nombres en la biblioteca, como \n" +
- "'System', 'System.IO',\n" +
- "'System.Drawing',\n" +
- "'System.Drawing.Drawing2D',\n" +
- "o 'System.Windows.Forms'.");
-
- label = new Label();
- label.Parent = this;
- label.Text = "Enumeraci≤n:";
- label.Location = new Point(8, 40);
- label.Size = new Size(56, 8);
-
- tbEnumeration = new TextBox();
- tbEnumeration.Parent = this;
- tbEnumeration.Text = "Barras de desplazamiento";
- tbEnumeration.Location = new Point(64, 40);
- tbEnumeration.Size = new Size(120, 12);
- tbEnumeration.Anchor |= AnchorStyles.Right;
-
- tooltip.SetToolTip(tbEnumeration,
- "Introduzca el nombre de una enumeraci≤n\n" +
- "definida en el espacio de nombres.");
-
- cbHex = new CheckBox();
- cbHex.Parent = this;
- cbHex.Text = "Hex";
- cbHex.Location = new Point(192, 16);
- cbHex.Size = new Size(40, 8);
- cbHex.Anchor = AnchorStyles.Top | AnchorStyles.Right;
- cbHex.CheckedChanged += new EventHandler(CheckBoxOnCheckedChanged);
-
- tooltip.SetToolTip(cbHex, "Marque esta casilla para mostrar los\n" +
- "valores de la enumeraci≤n en hexadecimal.");
-
- button = new Button();
- button.Parent = this;
- button.Text = "Aceptar";
- button.Location = new Point(192, 32);
- button.Size = new Size(40, 16);
- button.Anchor = AnchorStyles.Top | AnchorStyles.Right;
- button.Click += new EventHandler(ButtonOkOnClick);
-
- AcceptButton = button;
-
- tooltip.SetToolTip(button,
- "Haga clic en este bot≤n para mostrar los resultados.");
-
- tbOutput = new TextBox();
- tbOutput.Parent = this;
- tbOutput.ReadOnly = true;
- tbOutput.Multiline = true;
- tbOutput.ScrollBars = ScrollBars.Vertical;
- tbOutput.Location = new Point(8, 56);
- tbOutput.Size = new Size(226, 100);
- tbOutput.Anchor = AnchorStyles.Left | AnchorStyles.Top |
- AnchorStyles.Right | AnchorStyles.Bottom;
-
- AutoScaleBaseSize = new Size(4, 8);
-
- // Inicializar la presentaci≤n.
-
- ButtonOkOnClick(button, EventArgs.Empty);
- }
- void CheckBoxOnCheckedChanged(object sender, EventArgs ea)
- {
- button.PerformClick();
- }
- void ButtonOkOnClick(object sender, EventArgs ea)
- {
- FillTextBox(tbOutput, tbLibrary.Text, tbNamespace.Text,
- tbEnumeration.Text, cbHex.Checked);
- }
- public static bool FillTextBox(TextBox tbOutput, string strLibrary,
- string strNamespace,
- string strEnumeration, bool bHexadecimal)
- {
- string strEnumText = strNamespace + "." + strEnumeration;
- string strAssembly;
-
- try
- {
- strAssembly =
- Assembly.LoadWithPartialName(strLibrary).FullName;
- }
- catch
- {
- return false;
- }
- string strFullText = strEnumText + "," + strAssembly;
-
- // Obtener el tipo de enum
-
- Type type = Type.GetType(strFullText, false, true);
-
- if(type == null)
- {
- tbOutput.Text = "\"" + strFullText +
- "\" no es un tipo vßlido.";
- return false;
- }
- else if(!type.IsEnum)
- {
- tbOutput.Text = "\"" + strEnumText +
- "\" es un tipo vßlido pero no un enum.";
- return false;
- }
-
- // Obtener todos los miembros en ese enum.
-
- string[] astrMembers = Enum.GetNames(type);
- Array arr = Enum.GetValues(type);
- object[] aobjMembers = new object[arr.Length];
-
- arr.CopyTo(aobjMembers, 0);
-
- // Crear un StringBuilder para el texto.
-
- StringBuilder sb = new StringBuilder();
-
- // A±adir la enumeraci≤n y las cabeceras.
-
- sb.Append(strEnumeration);
- sb.Append(" Enumeration\r\nMember\tValue\r\n");
-
- // A±adir la presentaci≤n del texto y los valores numΘricos actuales.
-
- for (int i = 0; i < astrMembers.Length; i++)
- {
- sb.Append(astrMembers[i]);
- sb.Append("\t");
-
- if (bHexadecimal)
- sb.Append("0x" + Enum.Format(type, aobjMembers[i], "X"));
- else
- sb.Append(Enum.Format(type, aobjMembers[i], "D"));
-
- sb.Append("\r\n");
- }
- // A±adir otra informaci≤n.
-
- sb.Append("\r\nTotal = " + astrMembers.Length + "\r\n");
- sb.Append("\r\n" + type.AssemblyQualifiedName + "\r\n");
-
- // Configurar la propiedad Text de StringBuilder.
-
- tbOutput.Text = sb.ToString();
- tbOutput.SelectionLength = 0;
- return true;
- }
- }
-